package com.ydl.shopping.controller.visitor;

import com.ydl.shopping.core.controller.BaseController;
import com.ydl.shopping.core.dto.LoginDTO;
import com.ydl.shopping.core.dto.RegisterDTO;
import com.ydl.shopping.core.vo.ResultVO;
import com.ydl.shopping.service.ITbUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 登录相关接口
 *
 * @author 林武泰
 */
@RestController
@CrossOrigin
@RequestMapping("/visitor/user")
@Tag(name = "登录Controller")
public class LoginController extends BaseController {
    @Resource
    private ITbUserService tbUserService;

    /**
     * 登录
     *
     * @param loginDTO 登录信息
     * @return 结果
     * */
    @PostMapping("/login")
    @Operation(summary = "登录")
    @Parameter(description = "登录表单")
    public ResultVO login(@Validated @RequestBody LoginDTO loginDTO) {
        return tbUserService.login(loginDTO);
    }

    /**
     * 用户注册
     *
     * @param registerDTO 注册信息
     * @return 注册结果
     */
    @PostMapping("/register")
    @Operation(summary = "用户注册")
    @Parameter(description = "注册表单")
    public ResultVO register(@Validated @RequestBody RegisterDTO registerDTO) {
        String password = registerDTO.getPassword();
        // 密码加密
        registerDTO.setPassword(encodePassword(password));
        return tbUserService.register(registerDTO);
    }
}
